#ifndef TRANSACTION_TRANSACTION_H
#define TRANSACTION_TRANSACTION_H 1

#include <common/macro/build.h>
#include <common/cache.h>

#include <database/database.h>

#include <transaction/types.h>
#include <transaction/transactionlist.h>

typedef struct transaction_t {
	/* Transaction description */
	transaction_id_t id;
	transaction_phase_e phase;
	transaction_isolation_e isolation;

	/* Root database */
	database_t *database;

	/* Other transactions statuses */
	cache_t *abort_log_cache;
	transaction_list_t *transaction_list;

	/* Command iterator. */
	command_id_t command_id;
} transaction_t;

API_LIBLOCAL int transaction_to_active(transaction_t *);
API_LIBLOCAL int transaction_to_partially_commited(transaction_t *);
API_LIBLOCAL int transaction_to_commited(transaction_t *);
API_LIBLOCAL int transaction_to_failed(transaction_t *);
API_LIBLOCAL int transaction_to_aborted(transaction_t *);
API_LIBLOCAL int transaction_force_abort(transaction_t *);
API_LIBLOCAL void transaction_destroy(transaction_t *);
API_LIBLOCAL void transaction_refresh(transaction_t *);
API_LIBLOCAL int transaction_visibility(transaction_t *, transaction_id_t, transaction_phase_e *);
API_LIBLOCAL int transaction_deleteability(transaction_t *, transaction_id_t, transaction_phase_e *);
API_PUBLIC transaction_t *transaction_begin(database_t *, transaction_isolation_e);
API_PUBLIC int transaction_commit(transaction_t *);
API_PUBLIC int transaction_rollback(transaction_t *);

#endif
